package com.insurance.web;

import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import com.insurance.bean.Agent;
import com.insurance.bean.Company;
import com.insurance.bean.Product;
import com.insurance.service.AgentService;
import com.insurance.service.CompanyService;
import com.insurance.service.ProductService;
import com.insurance.util.PageBean;


public class ProductAction {
	@Resource ProductService productService;
	@Resource CompanyService companyService;
	@Resource AgentService agentService;
	private List<Product> products;
	private List<Agent> agents;
	private Product product;
	private Integer id;
	private Integer type;
	private PageBean page;
	
	public PageBean getPage() {
		return page;
	}
	public void setPage(PageBean page) {
		this.page = page;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}

	public List<Agent> getAgents() {
		return agents;
	}
	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	private List<Company> companys;
	
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	

	/**
	 * product Home 
	 */
	public String index(){
		this.companys = this.companyService.getCompanys();
		
		int cpage = 1;
		try {
			cpage = page.getCurrentpage();
		} catch (Exception e) {
			cpage = 1;
		}
		page = new PageBean(productService.getTotalCount(), cpage,
				PageBean.perpage);
		this.products = productService.getProductsByPage(page);
		
		return "index";
	}
	
	/**
	 * agent product
	 */
	public String alist(){
		Agent agent = (Agent)ServletActionContext.getRequest().getSession().getAttribute("agent");
		this.products = this.productService.getProductsByCompany(agent.getCompanyName());
		return "success";
	}
	
	/**
	 * agent proxy products
	 */
	public String plist(){
		Agent agent = (Agent)ServletActionContext.getRequest().getSession().getAttribute("agent");
		if(agent==null)
			return "back";
		else{
		this.products = this.productService.getProductsByAgent(agent.getId());
		return "list";
		}
	}	
	/**
	 * 产品列表
	 */
	public String list(){
		int cpage = 1;
		try {
			cpage = page.getCurrentpage();
		} catch (Exception e) {
			cpage = 1;
		}
		
		page = new PageBean(productService.getTotalCount(), cpage,
				PageBean.perpage);
		this.products = productService.getProductsByPage(page);
		return "list";
	}
	
	/**
	 * 根据类型查询产品
	 */
	public String type(){
		this.products = productService.getProductsByType(this.type);
		return "type";
	}

	/**
	 * 
	 * 显示单个产品
	 */
	public String showProduct(){
		this.product = this.productService.getProduct(this.id);
		this.agents = this.agentService.getAgentsCheckedTop5();
		return "show";
	}
	
	/**
	 * 查找产品
	 */
	public String search(){
		this.products = this.productService.searchProduct(product);
		return "show";
	}

	
	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}
	
}
